/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nanterre.annuaire.forStruts;

import com.nanterre.annuaire.bd.*;
import com.nanterre.annuaire.model.*;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author albran
 */
public class DeroulerElement extends org.apache.struts.action.Action {

    /*
     * forward name="success" path=""
     */
    

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        int parentID = Integer.parseInt(request.getParameter("parentID"));
        int grandParentID = Integer.parseInt(request.getParameter("grandParentID"));
        String type = request.getParameter("type");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        BDInteraction bdi = new BDInteraction();
        if("DOMAINE".equals(type)){
            List<Mention> listMention = bdi.getAllMentionsByIdDomaine(parentID);
            for(Mention current : listMention){
                    out.println(current.afficherMention(parentID));
            }
        }else if("COMPOSANTE".equals(type)){
            List<Diplome> listDiplome = bdi.getAllDiplomesByComposante(parentID);
            for(Diplome current : listDiplome){
                    out.println(current.afficherDiplome(parentID));
            }
        }else if("MENTION".equals(type)){
            List<Diplome> listDiplomes = bdi.getAllDiplomesByIdMention(parentID);
            for(Diplome current : listDiplomes){
                    out.println(current.afficherDiplome(parentID));
            }
        }else if("DIPLOME".equals(type)){
            List<Parcours> listParcours = bdi.getAllParcoursByParentId(parentID);
            for(Parcours current : listParcours){
                out.println(current.afficherParcours(parentID));
            }
        }else if("PARCOURS".equals(type)){
            List<UE> listUE = bdi.getAllUeByParentId(parentID);
            out.println("<table class=\"tableSemestre\" border=\"1\">");
            for(int i=1; i<=10; i++){ //pour chaque semestre
                boolean notExiste = true;
                for(int j=0; j<=8; j++){//pour chaque liste possible d'UE
                    if(j>0){
                        List<UE> listeTemp = new ArrayList<UE>();
                        for(UE current : listUE){
                            if(current.getNumListeUe() == j && current.getSemestre() == i){
                                listeTemp.add(current);
                            }
                        }
                        if(!listeTemp.isEmpty()){
                            if(notExiste){
                                out.println("<tr class=\"semestreTR\"><td class=\"semestre\">S"+i+"</td><td>");
                                notExiste = false;
                            }
                            out.println("<span style=\"color:red;font-style:italic;\">en choisir 1 parmis les "+listeTemp.size()+"</span>");
                            out.println("<div style=\"border:solid 1px #CCC;font-style:italic;margin:0 4% 2% 4%;\">");
                            for(UE current : listeTemp){
                                Float nbCredit = bdi.getCreditsUE(current.getId());
                                if(current.getLibelleComplet().equals(request.getSession().getAttribute("recherche"))){
                                    out.println(current.afficherUERecherche(parentID, nbCredit));
                                    request.getSession().removeAttribute("recherche");
                                }else{
                                    out.println(current.afficherUE(parentID, nbCredit));
                                }
                            }
                            out.println("</div>");
                        }
                    }else{
                        for(UE current : listUE){
                            if(current.getNumListeUe() == 0 && current.getSemestre() == i){
                                if(notExiste){
                                    out.println("<tr class=\"semestreTR\"><td class=\"semestre\">S"+i+"</td><td>");
                                    notExiste = false;
                                }
                                Float nbCredit = bdi.getCreditsUE(current.getId());
                                if(current.getLibelleComplet().equals(request.getSession().getAttribute("recherche"))){
                                    out.println(current.afficherUERecherche(parentID, nbCredit));
                                    request.getSession().removeAttribute("recherche");
                                }else{
                                    out.println(current.afficherUE(parentID, nbCredit));
                                }
                            }
                        }
                    }
                }
                out.println("</td>");
                out.println("</tr>");
            }
            out.println("</table>");
        }else if("UE".equals(type)){
            List<Ec> listEc = bdi.getAllEcByParentId(parentID, grandParentID);
            for(int i=0; i<=8; i++){
                if(i>0){
                    List<Ec> listeTemp = new ArrayList<Ec>();
                    for(Ec current : listEc){
                        if(current.getNumListeEc() == i){
                            listeTemp.add(current);
                        }
                    }
                    if(!listeTemp.isEmpty()){
                        String scroll = "";
                        if(listeTemp.size()>4){
                            scroll = "overflow:scroll;";
                        }
                        int minimum = bdi.getMinimumListeEc(parentID, i);
                        out.println("<span style=\"color:red;font-style:italic;\">Choisir "+minimum+" parmis les "+listeTemp.size()+"</span>");
                        out.println("<div style=\"border:solid 1px #CCC;font-style:italic;max-height:200px;margin-left:4%;"+scroll+"\">");
                        for(Ec current : listeTemp){
                            if(current.getLibelleComplet().equals(request.getSession().getAttribute("recherche"))){
                                out.println(current.afficherECRecherche());
                                request.getSession().removeAttribute("recherche");
                            }else{
                                out.println(current.afficherEC());
                            }
                        }
                        out.println("</div>");
                    }
                } else{
                    for(Ec current : listEc){
                        if(current.getNumListeEc() == 0){
                            if(current.getLibelleComplet().equals(request.getSession().getAttribute("recherche"))){
                                out.println(current.afficherECRecherche());
                                request.getSession().removeAttribute("recherche");
                            }else{
                                out.println(current.afficherEC());
                            }
                        }
                    }
                }
            }
        }
     
        out.flush();
        return null;
    }
}
